home *** CD-ROM | disk | FTP | other *** search
/ PC PowerPlay 22 / PCPP #22.iso / Quake2 / q2source_12_11 / utils3 / texpaint / texpaint.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-09-29  |  5.1 KB  |  291 lines

  1.  
  2. #include "texpaint.h"
  3.  
  4. triangle_t    *faces;
  5. int        numfaces;
  6.  
  7. int        skinwidth, skinheight;
  8. int        picwidth, picheight;
  9. int        width, height;
  10. int        iwidth, iheight;
  11. int        width2, height2;        // padded to ^2
  12.  
  13. float    tmcoords[10000][3][2];
  14.  
  15. byte        pic[1024*512];
  16. unsigned    rgb[1024*512];
  17.  
  18. float    scale;
  19. float    s_scale, t_scale;
  20.  
  21. char    filename[1024];
  22. char    picfilename[1024];
  23.  
  24.  
  25. /*
  26. ================
  27. BoundFaces
  28. ================
  29. */
  30. vec3_t    mins, maxs;
  31.  
  32. void BoundFaces (void)
  33. {
  34.     int        i,j,k;
  35.     triangle_t    *pol;
  36.     float    v;
  37.  
  38.     for (i=0 ; i<3 ; i++)
  39.     {
  40.         mins[i] = 9999;
  41.         maxs[i] = -9999;
  42.     }
  43.  
  44.     for (i=0 ; i<numfaces ; i++)
  45.     {
  46.         pol = &faces[i];
  47.         for (j=0 ; j<3 ; j++)
  48.             for (k=0 ; k<3 ; k++)
  49.             {
  50.                 v = pol->verts[j][k];
  51.                 if (v<mins[k])
  52.                     mins[k] = v;
  53.                 if (v>maxs[k])
  54.                     maxs[k] = v;
  55.             }    
  56.     }
  57.     
  58.     for (i=0 ; i<3 ; i++)
  59.     {
  60.         mins[i] = floor(mins[i]);
  61.         maxs[i] = ceil(maxs[i]);
  62.     }
  63.     
  64.     width = maxs[0] - mins[0];
  65.     height = maxs[2] - mins[2];
  66.     
  67.     printf ("width: %i  height: %i\n",width, height);
  68.  
  69.     if (!skinwidth)
  70.     {    // old way
  71.         scale = 8;
  72.         if (width*scale >= 150)
  73.             scale = 150.0 / width;    
  74.         if (height*scale >= 190)
  75.             scale = 190.0 / height;
  76.         s_scale = t_scale = scale;
  77.         iwidth = ceil(width*scale) + 4;
  78.         iheight = ceil(height*scale) + 4;
  79.     }
  80.     else
  81.     {    // new way
  82.         s_scale = (skinwidth/2-4)/(float)width;
  83.         t_scale = (skinheight-4)/(float)height;
  84.         iwidth = skinwidth/2;
  85.         iheight = skinheight;
  86.     }
  87.  
  88.     printf ("scale: %f\n",scale);
  89.     printf ("iwidth: %i  iheight: %i\n",iwidth, iheight);
  90. }
  91.  
  92.  
  93.  
  94. /*
  95. ============
  96. AddFace
  97. ============
  98. */
  99. void AddFace (int facenum, triangle_t *f)
  100. {
  101.     vec3_t        v1, v2, normal;
  102.     int        basex, basey;
  103.     int            i, j;
  104.     int        coords[3][2];
  105.  
  106. //
  107. // determine which side to map the teture to
  108. //
  109.     VectorSubtract (f->verts[0], f->verts[1], v1);
  110.     VectorSubtract (f->verts[2], f->verts[1], v2);
  111.     CrossProduct (v1, v2, normal);
  112.     
  113.     if (normal[1] > 0)
  114.         basex = iwidth + 2;
  115.     else
  116.         basex = 2;
  117.     basey = 2;
  118.  
  119.     for (i=0 ; i<3 ; i++)
  120.     {
  121.         coords[i][0] = Q_rint((f->verts[i][0] - mins[0])*s_scale + basex);
  122.         coords[i][1] = Q_rint( (maxs[2] - f->verts[i][2])*t_scale + basey);
  123. tmcoords[facenum][i][0] = coords[i][0]/(float)width2;
  124. tmcoords[facenum][i][1] = coords[i][1]/(float)height2;
  125.     }
  126.     
  127. }
  128.  
  129.  
  130. void CalcTmCoords (void)
  131. {
  132.     int        j;
  133.  
  134.     BoundFaces ();
  135.  
  136.     for (j=0 ; j<numfaces ; j++)
  137.         AddFace (j, &faces[j]);
  138.  
  139.     printf ("numfaces: %i\n",numfaces);
  140. }
  141.  
  142. //===============================================================================
  143.  
  144.  
  145.  
  146. #define    MAX_NUM_ARGVS    32
  147. int        argc;
  148. char    *argv[MAX_NUM_ARGVS];
  149.  
  150. /*
  151. ============
  152. ParseCommandLine
  153. ============
  154. */
  155. void ParseCommandLine (char *lpCmdLine)
  156. {
  157.     argc = 1;
  158.     argv[0] = "programname";
  159.  
  160.     while (*lpCmdLine && (argc < MAX_NUM_ARGVS))
  161.     {
  162.         while (*lpCmdLine && ((*lpCmdLine <= 32) || (*lpCmdLine > 126)))
  163.             lpCmdLine++;
  164.  
  165.         if (*lpCmdLine)
  166.         {
  167.             argv[argc] = lpCmdLine;
  168.             argc++;
  169.  
  170.             while (*lpCmdLine && ((*lpCmdLine > 32) && (*lpCmdLine <= 126)))
  171.                 lpCmdLine++;
  172.  
  173.             if (*lpCmdLine)
  174.             {
  175.                 *lpCmdLine = 0;
  176.                 lpCmdLine++;
  177.             }
  178.             
  179.         }
  180.     }
  181. }
  182.  
  183. /*
  184. =================
  185. LoadTriFile
  186. =================
  187. */
  188. void LoadTriFile (char *name)
  189. {
  190.     strcpy (tri_filename, name);
  191.     SetWindowText (camerawindow, tri_filename);
  192.  
  193.     LoadTriangleList (tri_filename, &faces, &numfaces);
  194.     InvalidateRect (camerawindow, NULL, false);
  195. }
  196.  
  197. /*
  198. ==================
  199. TimerProc
  200.  
  201. ==================
  202. */
  203. int CALLBACK TimerProc(
  204.     HWND hwnd,    // handle of window for timer messages 
  205.     UINT uMsg,    // WM_TIMER message
  206.     UINT idEvent,    // timer identifier
  207.     DWORD dwTime     // current system time
  208.    )
  209. {
  210.     static int    counter;
  211.     char        name[1024];
  212.  
  213.     if (!skin_filename[0])
  214.         return 0;
  215.  
  216.     if (!modified_past_autosave)
  217.     {
  218.         counter = 0;
  219.         return 0;
  220.     }
  221.  
  222.     counter++;
  223.  
  224.     if (counter < 3*5)
  225.         return 0;        // save every five minutes
  226.  
  227.     strcpy (name, skin_filename);
  228.     StripExtension (name);
  229.     strcat (name, "_autosave.lbm");
  230.     Skin_SaveFile (name);
  231.  
  232.     modified_past_autosave = false;
  233.     counter = 0;
  234.  
  235.     return 0;
  236. }
  237.  
  238. /*
  239. ==================
  240. WinMain
  241.  
  242. ==================
  243. */
  244. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance
  245.                     ,LPSTR lpCmdLine, int nCmdShow)
  246. {
  247.     MSG            msg;
  248.     HACCEL        accelerators;
  249.  
  250.     main_instance = hInstance;
  251.  
  252.     ParseCommandLine (lpCmdLine);
  253.  
  254.     screen_width = GetSystemMetrics (SM_CXFULLSCREEN);
  255.     screen_height = GetSystemMetrics (SM_CYFULLSCREEN);
  256.  
  257.     // hack for broken NT 4.0 dual screen
  258.     if (screen_width > 2*screen_height)
  259.         screen_width /= 2;
  260.  
  261.     accelerators = LoadAccelerators (hInstance
  262.         , MAKEINTRESOURCE(IDR_ACCELERATOR1));
  263.     if (!accelerators)
  264.         Sys_Error ("LoadAccelerators failed");
  265.  
  266.     Main_Create (hInstance);
  267.     WCam_Create (hInstance);
  268.     WPal_Create (hInstance);
  269.     WSkin_Create (hInstance);
  270.  
  271.     if (argc == 2)
  272.         Skin_LoadFile (argv[1]);
  273.  
  274.     SetTimer ( mainwindow, 1, 1000*20, TimerProc );
  275.  
  276.     while (1)
  277.     {
  278.         if (!GetMessage (&msg, mainwindow, 0, 0))
  279.             break;
  280.         if (!TranslateAccelerator(mainwindow, accelerators, &msg) )
  281.         {
  282.               TranslateMessage (&msg);
  283.               DispatchMessage (&msg);
  284.         }
  285.     }
  286.  
  287.     /* return success of application */
  288.     return TRUE;
  289. }
  290.  
  291.